package com.xxx.config;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import com.xxx.common.utils.DateUtil;

import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

/**
 * @ClassName: LongTimeToDate 
 * @Description: FreeMarker 自定义方法 long类型时间格式化
 */
public class LongTimeToDate implements TemplateMethodModelEx {

	@SuppressWarnings("rawtypes")
	@Override
	public Object exec(List args) throws TemplateModelException {
		int size = args.size();
		if (size < 1) {
			throw new TemplateModelException("参数错误!");
		}
		Object obj = args.get(0);
		if (obj == null) {
			return "";
		}
		String date = obj.toString()+"00000000000000";
		if(NumberUtils.isNumber(date)){
			long longdate = NumberUtils.toLong(date.substring(0, 14));
			if(longdate <= 9999){
				longdate = DateUtil.dateToLong();
			}
			if(size == 1){
				return DateUtil.longToString(longdate, DateUtil.DATE_FORMAT_DATE_S);
			}
			if(size == 2){
				SimpleDateFormat dateFormat = new SimpleDateFormat(args.get(1).toString());
				return DateUtil.longToString(longdate, dateFormat);
			}
		}
		return date;
		
	}

}
